package cn.tedu.mimall.product.controller;

import cn.tedu.mimall.product.pojo.dto.ClassifyAddNewDTO;
import cn.tedu.mimall.product.pojo.dto.ClassifyUpdateDTO;
import cn.tedu.mimall.product.pojo.vo.ClassifyListItemVO;
import cn.tedu.mimall.product.pojo.vo.ClassifyStandardVO;
import cn.tedu.mimall.product.service.IClassifyService;
import cn.tedu.mimall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/classifies")
@Slf4j
@Api(tags = "分类控制器")
public class ClassifyController {

    @Autowired
    IClassifyService classifyService;

    public ClassifyController() {
        log.debug("分类控制器ClassifyController，启动！");
    }

    @ApiOperation("添加分类")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add")
    public JsonResult<Void> addClassify(ClassifyAddNewDTO classifyAddNewDTO){
        log.debug("开始处理【添加分类】的请求，参数：{}",classifyAddNewDTO);
        classifyService.insertAdd(classifyAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除分类")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(value = "分类id",name = "id")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id){
        log.debug("开始处理【删除分类】的请求，参数：{}",id);
        classifyService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改分类")
    @ApiOperationSupport(order = 300)
    @PostMapping("/update")
    public JsonResult<Void> update(Long id, ClassifyUpdateDTO classifyUpdateDTO){
        log.debug("开始处理【修改分类】的请求，参数：{}，{}",id,classifyUpdateDTO);
        classifyService.update(id,classifyUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("分类列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("/list")
    public JsonResult<List<ClassifyListItemVO>> list(){
        log.debug("开始处理【查询分类列表】的请求");
        List<ClassifyListItemVO> list = classifyService.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("查询分类数据")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(value = "分类id",name = "id")
    @PostMapping("/{id:[0-9]+}/get")
    public JsonResult<ClassifyStandardVO> get(@PathVariable Long id){
        log.debug("开始处理【查询分类】的请求，参数：{}",id);
        ClassifyStandardVO classify = classifyService.getById(id);
        return JsonResult.ok(classify);
    }

}
